<include file="header" />
<body>
<div class="wraper">
    <div class="load">
        <img class="load-bee" src="/assets/base/repair/images/load-bee.png"/>

    </div>
    <div class="top">
        <p>
        	<a href="javascript:history.back(-1)" class="iconfont icon-zuo fl-left"></a>
        	地址管理
        	<a href="{BEESCRM::U('base/repair/orderlist',array('wechat_id'=>$wechat_id,'role'=>'fans'))}" class="iconfont icon-shouye fl-right"></a>
        </p>
    </div>
    <p class="title">生活区</p>
    <p class="form-box">
        <select class="select" id="community_id">
        	<option value="0">请选择所在生活区</option>
            <volist name="community" id="vo">
            	<option value="{BEESCRM:$vo.community_id}" <if condition="$data['community_id'] eq $vo['community_id']"> selected="selected"</if>>{BEESCRM:$vo.community_name}</option>
            </volist>
        </select>
        <span class="triangle"></span>
    </p>
    <p class="title">寝室楼</p>
    <p class="form-box">
        <select class="select" id="building_id">
      		<if condition="$data['building_id']">
      			<volist name="building" id="vo">
        		<option value="{BEESCRM:$vo['building_id']}" <if condition="$data['building_id'] eq $vo['building_id']">selected="selected"</if>>{BEESCRM:$vo['building_name']}</option>
        		</volist>
        	<else/>
            	<option value="0">请选择所在寝室楼</option>
            </if>
        </select>
        <span class="triangle"></span>
    </p>
    <p class="title">寝室号</p>
    <p class="form-box">
        <input class="input" value="{BEESCRM:$data.room}" id="room"/>
    </p>
    <p class="title">姓名</p>
    <p class="form-box">
        <input class="input" value="{BEESCRM:$data.username}" id="username"/>
    </p>
    <p class="title">联系方式</p>
    <p class="form-box">
        <input class="input" value="{BEESCRM:$data.mobile}" id="mobile"/>
    </p>
    <div class="marginb-50"></div>
    <div class="btn-box">
    	<input type="hidden" id="order_id" value="{BEESCRM:$order_id}">
    	<input type="hidden" id="address_id" value="{BEESCRM:$address_id}">
        <a href="javascript:void(0)" class="btn big" id="addAddr">提交</a>
    </div>
</div>

<include file="commonJS" />
<script>
    $(function(){
        $('.list-box').on('click','.list',function(){
            $(this).addClass('check').siblings().removeClass('check');
        })
    })
    $("#community_id").change(function () {
		var id = $("#community_id").val();
		var name = getParams(id);
		var options = [];
		if(Number(name)==0){
			option = '<option value="0">请选择楼号</option>';
			options.push(option);
		}else{
			$.each(name, function (index, node) {
				option = '<option value="' + node.building_id+'">' + node.building_name + '</option>';
				options.push(option);
			});
		}
		$("#building_id").html(options.join(''));
	});
	function getParams(id, colname, table_name) {
		id = id || null;
		colname = colname || 'community_id';
		table_name = table_name || 'app_repair_building';
		var result = null;
		$.ajax({
			url: "{BEESCRM::U('base/Repair/getParams',array('wechat_id'=>$wechat_id))}",
			type: "post",
			dataType: 'json',
			async: false,
			data: {
				id: id,
				colname: colname,
				table_name: table_name
			},
			success: function (data) {
				if (data.status == 0) {
					result = data.result;
				}
			}
		});
		return result;
	}
    $('#addAddr').on('click',function(){
    	var order_id=$('#order_id').val();
		var address_id=$('#address_id').val();
        var username = $('#username').val();
        var mobile = $('#mobile').val();
        var verify_method = /1[3-8]+\d{9}/;
        var room = $('#room').val();
        var community_id = $("#community_id").val();
        var community_name = $("#community_id").find("option:selected").text();
        var building_id = $("#building_id").val();
        var building_name = $("#building_id").find("option:selected").text();
        if (!community_id || community_id=='0') {
        	$.alert('error','请选择生活区');
        	return false;
        }else if(!building_id || building_id=='0'){
        	$.alert('error','请选择寝室楼');
        	return false;
       	}else if (!room) {
       		$.alert('error','请选择寝室号');
       		return false;
        }else if (username == '') {
        	$.alert('error','请输入姓名');
        	return false;
        }else if (!verify_method.test(mobile)) {
        	$.alert('error','请输入正确的手机号');
        	return false;
        }else{
            var data = {
            	'address_id':address_id,
                'username': username,
                'mobile': mobile,
                'community_id': community_id,
                'community_name': community_name,
                'building_id': building_id,
                'building_name': building_name,
                'room': room,
            };
            $.ajax({
                url: "{BEESCRM::U('base/Repair/addAddress',array('wechat_id'=>$wechat_id))}",
                type: 'post',
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data.status == 'S') {
                    	$.alert('success',data.message);
                       	setTimeout("location.href ='{BEESCRM::U('base/Repair/address',array('wechat_id'=>$wechat_id))}/order_id/"+order_id+"'", 2000);
                    } else {
                    	$.alert('error',data.message);
                    	return false;
                    }
                }
            });
        }
    });
</script>
</body>

</html>
